<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <!-- 头部工具组件 -->
        <pageTools>
          <template #before>
            <el-button type="primary" size="small" @click="addCustomer">添加客户</el-button>
          </template>
          <template #after>
            <el-select v-model="agents">
              <el-option
                :value="null"
                label="全部经纪"
              />
              <el-option
                v-for="item in agentsList"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              />
            </el-select>
            <el-input v-model="searchWord" placeholder="请输入关键字" class="input-with-select">
              <el-button slot="append" type="primary" class="searchBtn" @click="search">筛选</el-button>
            </el-input>
          </template>
        </pageTools>
      </template>
      <!-- 表格 -->
      <el-table
        :data="customerData"
        border
        style="width: 100%"
      >
        <el-table-column prop="image" label="图片" width="220" align="center">
          <template v-slot="{row}" class="imgbox">
            <img
              v-errImg="require('@/assets/404_images/404_cloud.png')"
              :src="row.image"
              alt=""
              class="image"
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          align="center"
        />
        <el-table-column
          prop="email"
          label="邮箱"
          align="center"
        />
        <el-table-column
          prop="phone"
          label="电话"
          align="center"
        />
        <el-table-column
          prop="agent.name"
          label="经纪"
          align="center"
        />
        <el-table-column
          prop="wechat"
          label="微信"
          align="center"
        />
        <el-table-column
          prop="desc"
          label="介绍"
          align="center"
        />
        <el-table-column label="操作" align="center" width="300">
          <template v-slot="{row}">
            <el-button type="primary" size="small" @click="editCustomer(row.id)">编辑</el-button>
            <el-button type="danger" size="small" @click="delCustomer(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        :page-size="size"
        layout="prev, pager, next"
        :total="total"
        @current-change="currentChange"
      />
    </el-card>
    <!-- 添加/编辑客户 弹窗 -->
    <AddCustomer ref="addCustomer" :show-dialog.sync="visibleDIalog" @updateEvent="getClientsList" />
  </div>
</template>

<script>
import { clientsList, clientsCount, deleteClients } from '@/api/customer'
import { economicList } from '@/api/economic'
import AddCustomer from './components/addCustomer.vue'
export default {
  components: { AddCustomer },
  data() {
    return {
      customerData: [],
      size: 2, // 每页显示多少条数据
      total: 0, // 客户列表总数
      page: 0, // 页码
      searchWord: '', // 搜索关键字
      agents: null, // 经纪人
      visibleDIalog: false, // 控制添加客户弹窗显示隐藏
      agentsList: [] // 经纪人列表
    }
  },
  async created() {
    this.getClientsList()
    // 获取经纪人列表
    this.agentsList = await economicList({
      _limit: -1
    })
    // 子组件获取经纪列表数据
    this.$refs.addCustomer.agentsList = this.agentsList
  },
  methods: {
    // 获取客户列表
    async getClientsList() {
      if (this.searchWord || this.agents) {
        const res = await clientsList({
          _limit: this.size,
          _start: this.page,
          name_contains: this.searchWord,
          agent_contains: this.agents
        })
        this.customerData = res
        // 获取客户总数量
        const result = await clientsCount({
          _limit: this.size,
          _start: this.page,
          name_contains: this.searchWord,
          agent_contains: this.agents
        })
        this.total = result
      } else {
        const res = await clientsList({
          _limit: this.size,
          _start: this.page
        })
        this.customerData = res
        // 获取客户总数量
        const result = await clientsCount({
          _limit: this.size,
          _start: this.page
        })
        this.total = result
      }
    },
    // 分页功能
    currentChange(val) {
      this.page = (val - 1) * 2
      this.getClientsList()
    },
    // 筛选
    search() {
      this.getClientsList()
    },
    // 添加客户
    addCustomer() {
      this.visibleDIalog = true
    },
    // 编辑客户
    async editCustomer(id) {
      this.visibleDIalog = true
      // 根据id 实现数据回显
      this.$refs.addCustomer.getClientById(id)
    },
    // 删除客户
    async delCustomer(id) {
      // 询问框
      this.$confirm('您确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用删除分类接口
        await deleteClients(id)
        // 判断删除的数据是否为最后一条数据
        if (this.customerData.length === 1 && this.page !== 0) {
          this.page -= 2
        }
        // 重新渲染页面
        this.getClientsList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((err) => {
        console.log(err)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-pagination{
    text-align: center;
    margin-top: 20px;
}
.image{
    width: 200px;
    height: 200px;
    object-fit: cover;
}
</style>
